<?php
/**
 * UploadFilesInfoAction class file.
 *
 * @package   controllers
 * @subpackage patientSignal
 * @since     1.0
 * @filesource
 */

Yii::import('application.controllers.patientSignal.AbstractUploadAction');

/**
 * 
 * Actin class to save signal file on server temporarily.
 * This action class will be integrated by PatientSignalController.
 * If file name is not valid or exists, error message array will be returned; otherwise, reutrn success message array.
 * @see PatientSignalController 
 */
class UploadFilesInfoAction extends AbstractUploadAction {

	/**
	 * Ajax callback method to pass the orginal file name to the server and returns current session files for user selection.
	 */
	public function run(){
		if(!$this->isLegalFileName($_POST['name'])){
			echo CJSON::encode(array(
				'success'=>false,
				'message'=>'The file '.$_POST['name']. ' contains non-ascii character. Please use ascii character: a-z A-Z 0-9, these special chars: dot .  brackets: ( ) [ ] dash: - underscore: _ only.',
			));
			$this->removeSessionFile($_POST['id']);
		}elseif($this->sessionFileNameExist($_POST['name'])){
			echo CJSON::encode(array(
				'success'=>false,
				'message'=>'The file '.$_POST['name']. ' exist in current upload. Please choose a file with different name.',
			));
			$this->removeSessionFile($_POST['id']);
		}else{
			$this->setSessionFileName($_POST['id'], $_POST['name']);
			echo CJSON::encode(array(
				'success'=>true,
				'fileId'=>$_POST['id'], 
				'filename'=>$_POST['name'],
			));
		}
	}
}